<template>
    <div class="my">
        <div class="Nav">
            <img src="../assets/images/my-msg.png" />
        </div>
        <div class="head-name">
            <!-- 上传头像组件 -->
            <!-- <van-uploader :after-read="afterRead"> -->
            <!-- <div class="head" v-if="isLogined && userImg !== null">
                    <img class="img-auto" :src="userImg" />
                </div>
                <div class="head-box" v-else>
                    <img class="img-auto" src="../assets/images/user-head02.png" />
                </div> -->
            <div class="head">
                <img class="img-auto" :src="userImg" />
                <input type="file" class="input" name="image" accept="image/*" @change="changeImg" />
            </div>
            <!-- </van-uploader> -->

            <div class="name-box" v-if="isLogined">{{ userphone.replace(/^(.{3})(?:\d+)(.{4})$/, "$1****$2") }}</div>
            <div class="name-box" @click="goLogin" v-else>点击登录</div>
        </div>
        <div class="balance-signIn">
            <!-- 余额 -->
            <div class="balance-box" @click="clickBalance()">
                <div class="balance">余额</div>
                <div class="desc">充值送红包</div>
                <div class="Pay">充值</div>
            </div>
            <!-- 签到 -->
            <div class="signIn-box" @click="clickSignIn()">
                <div class="signIn">签到</div>
                <div class="desc">做任务领红包</div>
                <div class="sign-icon">签到</div>
            </div>
        </div>
        <!-- 操作 -->
        <div class="operations">
            <div class="operations-item">
                <img src="../assets/images/note.png" />
                <div class="text">笔记</div>
                <van-icon name="arrow" />
            </div>
            <div class="operations-item">
                <img src="../assets/images/shangchuang.png" />
                <div class="text">已购和上传</div>
                <van-icon name="arrow" />
            </div>
            <div class="operations-item">
                <img src="../assets/images/kefu.png" />
                <div class="text">在线客服</div>
                <van-icon name="arrow" />
            </div>
            <div class="operations-item">
                <img src="../assets/images/night.png" />
                <div class="text">夜间模式</div>
                <van-switch v-model="checked" size="24px" />
            </div>
            <div class="operations-item">
                <img src="../assets/images/jifen.png" />
                <div class="text">积分商城</div>
                <van-icon name="arrow" />
            </div>
            <div class="operations-item" @click="set">
                <img src="../assets/images/set.png" />
                <div class="text">设置</div>
                <van-icon name="arrow" />
            </div>
        </div>
    </div>
</template>

<script>
export default {
    name: "My",
    data() {
        return {
            checked: false,
            userphone: null,
            isLogined: null,
            userImg: require("../assets/images/user-head02.png"),
        };
    },
    created() {
        // 获取登录信息的手机号
        if (localStorage.getItem("userMsg") !== null) {
            this.userphone = JSON.parse(localStorage.getItem("userMsg")).phone;
        }
        // 获取登录标识符
        this.isLogined = JSON.parse(localStorage.getItem("isLogined"));

        // 获取blob数据

        if (localStorage.getItem("userImg") !== null) {
            this.userImg = localStorage.getItem("userImg");
        } else {
            this.userImg = require("../assets/images/user-head02.png");
        }

        if (!this.isLogined) {
            this.userImg = require("../assets/images/user-head02.png");
        }
    },
    mounted() {},
    methods: {
        goLogin() {
            this.$router.push({
                name: "Login",
            });
        },
        set() {
            this.$router.push({ name: "Set" });
        },
        // 点击余额
        clickBalance() {
            if (this.isLogined == false) {
                this.goLogin();
            }
        },
        // 点击签到
        clickSignIn() {
            if (this.isLogined == false) {
                this.goLogin();
            }
        },
        // 修改图片
        changeImg(e) {
            // 修改头像url
            if (e.target.files.length > 0) {
                // 获取blob数据

                window.URL = window.URL || window.webkitURL;
                this.userImg = window.URL.createObjectURL(e.target.files[0]);

                // 存入本地
                localStorage.setItem("userImg", this.userImg);
            } else {
                return;
            }
        },
    },
};
</script>

<style lang="less" scoped>
.my {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
    height: calc(100% - 50px);
    background-color: #f7f8f8;
    .operations {
        width: 350px;
        margin: 0 auto;
        .operations-item {
            height: 40px;
            display: flex;
            align-items: center;
            img {
                width: 22px;
                height: 22px;
            }
            .text {
                width: 100px;
                margin: 0 190px 0 15px;
                font-size: 15px;
                color: #8d9292;
            }
            .van-icon-arrow {
                color: #bac6c6;
                font-size: 16px;
            }
        }
    }
    .Nav {
        width: 100%;
        height: 50px;
        display: flex;
        justify-content: flex-end;
        align-items: center;
        img {
            width: 33px;
            height: 33px;
            margin: 0 10px 0 0;
        }
    }
    .balance-signIn {
        width: 350px;
        margin: 30px auto;
        display: flex;
        // box-shadow: 0px 1px 1px 1px #686969;
        box-shadow: 0 3px 1px #e2ebeb;

        .balance-box,
        .signIn-box {
            width: 50%;
            height: 100%;
            padding-bottom: 20px;
        }
        .balance-box {
            display: flex;
            flex-direction: column;
            align-items: center;
            .balance {
                font-weight: 700;
                font-size: 16px;
            }
            .desc {
                margin: 10px 0 10px 0px;
                color: #686969;
            }
            .Pay {
                width: 60px;
                height: 20px;
                border: 1px solid #444;
                border-radius: 10px;
                background-color: #e4393c;
                line-height: 20px;
                text-align: center;
                color: #fff;
            }
        }
        .signIn-box {
            display: flex;
            flex-direction: column;
            align-items: center;
            .signIn {
                font-weight: 700;
                font-size: 16px;
            }
            .desc {
                margin: 10px 0 10px 0px;
                color: #686969;
            }
            .sign-icon {
                width: 60px;
                height: 20px;
                border-radius: 10px;
                border: 1px solid #686969;
                line-height: 20px;
                text-align: center;
                color: #e4393c;
                background-color: #fff;
            }
        }
    }
    .head-name {
        width: 350px;
        margin: 0 auto;
        display: flex;
        align-items: center;
        .head-box {
            width: 70px;
            height: 70px;
            background-color: pink;
            border-radius: 50%;
            border: 1px solid #686969;
            margin-left: 10px;
        }
        .head {
            width: 70px;
            height: 70px;
            border-radius: 50%;
            border: 1px solid #686969;
            margin-left: 10px;
            position: relative;
            overflow: hidden;
            .input {
                width: 100%;
                height: 100%;
                position: absolute;
                top: 0;
                opacity: 0;
            }
        }
        .name-box {
            font-size: 20px;
            margin: 0 0 0 15px;
            font-weight: 700;
        }
    }
}
</style>
